package net.edu_soft.ec.asset.controller;


import com.github.pagehelper.PageInfo;
import net.edu_soft.ec.asset.pojo.Role;
import net.edu_soft.ec.asset.service.RoleService;
import net.edu_soft.ec.common.util.ResultVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: PUING
 * @Date: 2020/6/10 20:34
 * @Description: 角色控制类
 */
@RestController
@RequestMapping(value = "/role")
public class RoleController {

    @Autowired
    private RoleService roleService;


    /***
     * 查询角色信息
     * @param roleId 角色id
     * @return 角色信息
     */
    @RequestMapping(value = "/find")
    @RequiresPermissions("role:find")
    public ResultVo find(Integer roleId) {
        Role role = roleService.find(roleId);
        if (role == null) {
            return ResultVo.failed(400, "查询失败，角色信息为空", role);
        }
        return ResultVo.succeed("查询成功", role);
    }


    /***
     * 分页查询角色列表
     * @param pageNum 当前页
     * @param pageSize 每页数
     * @return 分页信息
     */
    @RequestMapping(value = "/findList")
    @RequiresPermissions("role:findList")
    public ResultVo findList(Integer pageNum, Integer pageSize) {
        PageInfo<Role> list = roleService.findList(pageNum, pageSize);
        return ResultVo.succeed("查询结果", list);
    }


    /***
     * 添加角色信息
     * @param name 角色名
     * @param details 角色详情
     * @return
     */
    @RequestMapping(value = "/save")
    @RequiresPermissions("role:save")
    public ResultVo save(String name, String details) {
        String m = roleService.save(name, details) == 1 ? "添加成功" : "添加失败";
        return ResultVo.succeed(m, null);
    }


    /***
     * 修改角色信息
     * @param role 角色信息
     * @return
     */
    @RequestMapping(value = "/update")
    @RequiresPermissions("role:update")
    public ResultVo update(Role role) {
        String m = roleService.update(role) == 1 ? "修改成功" : "修改失败";
        return ResultVo.succeed(m, null);
    }


    /***
     *
     * @param roleId
     * @param isDel
     * @return
     */
    @RequestMapping(value = "/del")
    @RequiresPermissions("role:del")
    public ResultVo del(Integer roleId,Integer isDel) {
        String m = roleService.del(roleId,isDel) >= 1 ? "删除成功" : "删除失败";
        return ResultVo.succeed(m, null);
    }
}
